<?php


require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/workerman/gatewayclient/Gateway.php';
use GatewayClient\Gateway;

class Server extends CI_Controller {


    //发送消息
	public function send(){
     $data=$this->input->post();
     Gateway::$registerAddress = '127.0.0.1:1239';
     $res=Gateway::sendToUid($data['to'],json_encode($data));
         $data['create_time']=time();
     $this->db->insert('chat',$data);
     $this->show(['code'=>1,'msg'=>'发送成功']);
	}


    //查询过往记录
	public function chatlog(){
	$data=$this->input->post();
	
	if($data['from']==$data['to']){
		    $list=$this->db->query("SELECT * FROM `ci_chat` WHERE ( `from` = ".$data['from'].") AND ( `to` = ".$data['from'].") order by create_time desc")->result_array();
	}else{
	    $list=$this->db->query("SELECT * FROM `ci_chat` WHERE ( `from` = ".$data['from']." OR `to` = ".$data['from']." ) AND ( `to` = ".$data['to']." OR `from` = ".$data['to']." ) order by create_time desc")->result_array();	
	}

    
    
    $this->show(['code'=>0,'msg'=>$list]);
	}


    //获取所以联系人
	public function userlist(){
	$post=$this->input->post();	
	$list=$this->db->group_by("from", "desc")->order_by("create_time", "desc")->get('chat')->result_array();
	foreach ($list as $k=>$v) {
	$list[$k]['last']=$this->db->where('from',$v['id'])->order_by("create_time", "desc")->get('chat')->row_array();
	}
     $this->show(['code'=>0,'msg'=>$list]);
	}


    //添加
	public function add(){
     $post=$this->input->post();
     $this->db->insert('info',$post);
    $this->show(['code'=>0,'msg'=>'添加成功']);
	}



    //查看
	public function look(){
     $post=$this->input->post();
     $list=$this->db->where('id',$post['id'])->get('info')->row_array();
     $this->show(['code'=>0,'msg'=>$list]);
	}



    //编辑
	public function edit(){
     $post=$this->input->post();
      $this->db->update('info',$post,array('id'=>$post['id']));	
   	if ($this->db->trans_status() === FALSE){
	$this->db->trans_rollback();
	$this->show(['code'=>0,'msg'=>'更新失败']);
	}else{
	$this->db->trans_commit();
	$this->show(['code'=>1,'msg'=>'更新成功']);
	}
	}



    //删除
	public function del(){
     $post=$this->input->post();
     $this->db->delete('info', array('id'=>$post['id']));
     
	if ($this->db->trans_status() === FALSE){
	$this->db->trans_rollback();
	$this->show(['code'=>0,'msg'=>'删除失败']);
	}else{
	$this->db->trans_commit();
	$this->show(['code'=>1,'msg'=>'删除成功']);
	}
  
	}



    //常用语列表
	public function infolist(){
     $post=$this->input->post();
     $list=$this->db->get('info')->result_array();
    $this->show(['code'=>0,'msg'=>$list]);
	}



    // 升序
	public function dosort(){
    $post=$this->input->post();
    $info=$this->db->where('id',$post['id'])->get('info')->row_array();//要移动的
    $change=$this->db->where('id >',$post['id'])->order_by("id", "asc")->get('info')->row_array();//被移动的
    
    $data['id']=$change['id'];
    $data['info']=$info['info'];
    
    $data2['id']=$info['id'];
    $data2['info']=$change['info'];
    
    $this->db->delete('info', array('id'=>$info['id']));
    $this->db->delete('info', array('id'=>$change['id']));
   
    $this->db->insert('info',$data);
    $this->db->insert('info',$data2);
    $this->show(['code'=>0,'msg'=>'已上移']);
	}



    //上传文件
	public function upload(){

     	$pathurl='file/'.date('Y-m-d');
		if (!file_exists($pathurl)) {
			mkdir($pathurl,0777,true);
		}
		$config['upload_path']      = $pathurl;
		$config['allowed_types']    = 'gif|jpg|png|jpeg|mp4';
		$config['file_name']    =date('Ymd',time()).uniqid();
		$config['max_size']     = 2048;
		$this->load->library('upload', $config);
			if (!$this->upload->do_upload('file')) {
			$data = array('code'=>0,'error'=>$this->upload->display_errors());
		} else {
			$data = array('code'=>1,'url'=>$pathurl.'/'.$config['file_name'],'upload_data'=>$this->upload->data());
		}

		 if ($data['code']==1){
			$file['url']=$data['url'].$data['upload_data']['file_ext'];
			$file['ext']=$data['upload_data']['file_ext'];
			$this->db->insert('file', $file);
			$id=$this->db->insert_id();
			$this->show(['code'=>1,'url'=>'https://'.$_SERVER['HTTP_HOST'].'/'.$file['url'],'img_id'=>$id]);
		}else{
			$this->show(['code'=>0,'msg'=>'上传失败']);
		}
	}




	public function show($data){
		echo json_encode($data);
		exit;
	}

	

    //  public function dologin(){
     	 
    //      $post['password']=md5($post['password'].'yw');
    //      $check=$this->db->where($post)->get('admin')->row_array();
    //      if ($check) {
    //       $this->load->library('session');
    //       $this->session->set_userdata('userinfo',$check);
          
    //       $data['session_id']=session_id();
    //       $data['update_time']=time();
    //       $data['ip']=$_SERVER['REMOTE_ADDR'];
    //       $this->db->where($post)->update('admin',$data);
       
    //       echo json_encode(array('code'=>1,'msg'=>'登录成功'));
    //       exit();
        
    //      }else {
    //       echo json_encode(array('code'=>0,'msg'=>'账号或密码错误'));
    //       exit();
    //      }
    //  }


    // public function logout(){
    // 	$this->load->library('session');
    // 	$this->session->unset_userdata('userinfo');
    // 		header("Location:/login/index");

    // }
    
    // public function error(){
    // $this->load->library('session');
    // $info=$this->session->userdata('userinfo');	
    // if(is_null($info)){
    // header("Location:/login/index");
    // }else{
    // $data['userinfo']=$this->db->where('id',$info['id'])->get('admin')->row_array();
    // $this->session->unset_userdata('userinfo');
    // $this->load->view('/login/error',$data);
    //  }
    // }
}
